﻿using CoreSimpleModel;
using DotNetExtensions.Core.Autofac;
using DotNetExtensions.Core.Jobs;
using DotNetExtensions.Core.Quartz;
using DotNetExtensions.Core.WebApi.Jwt;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoreWebSimple.Pages
{
    public class IndexModel : PageModel
    {
        private readonly Provider<string> _provider;
        private readonly IRepository _repository;
        private readonly IJwtValidateService<UserModel> _validateService;

        public List<JobInfo> Jobs { get; set; }

        public IndexModel(Provider<string> provider, IRepository repository, IJwtValidateService<UserModel> validateService)
        {
            _validateService = validateService;
            _provider = RegistTypeResolver.Resolve<Provider<string>>();
            var provider3 = RegistTypeResolver.Resolve<Provider3<Model, int>>();
            provider3.Write(new Model { Id = 1, Name = "Im jack" });
            _repository = repository;

            var named = RegistTypeResolver.ResolveNamed<Named>();
            foreach (var item in named)
            {
                item.Exec();
            }
        }

        public async void OnGetAsync()
        {
            string token = _validateService.CreateJwtToken(new JwtParameters(new List<JwtParameter>
            {
                 new JwtParameter(JwtParameters.Name, "jiaohxiong"),
                 new JwtParameter(JwtParameters.Sid, "1"),
            }), 1, out string audience, out DateTime expiredTime);
            ViewData.Add("Token", token);
            //Console.WriteLine(_provider.Write("Hello im string data"));
            //QuartzBuilder.Add(typeof(PrintJob3));
            //QuartzBuilder.Default.Build();
            // _repository.Write();

            Jobs = QuartzBuilder.Default.JobInfos;

            if(HttpContext.Request.Query.TryGetValue("handler",out StringValues value))
            {
               await QuartzBuilder.Remove(value[0]);
            }
        }

        public void OnPostPauseAsync()
        {

        }
    }
}
